import pymysql

# 数据库连接配置
config = {
    'host': 'localhost',
    'port': 3306,
    'user': 'root',
    'password': '123456',
    'charset': 'utf8mb4'
}

def create_database():
    try:
        # 连接到MySQL服务器（不指定数据库）
        connection = pymysql.connect(**config)
        cursor = connection.cursor()
        
        # 创建数据库
        cursor.execute("CREATE DATABASE IF NOT EXISTS educate_ai_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
        print("数据库 educate_ai_db 创建成功！")
        
        # 显示当前所有数据库
        cursor.execute("SHOW DATABASES")
        databases = cursor.fetchall()
        print("当前所有数据库：")
        for db in databases:
            print(f"- {db[0]}")
            
        # 关闭连接
        cursor.close()
        connection.close()
        
    except pymysql.MySQLError as e:
        print(f"数据库操作失败: {e}")
    except Exception as e:
        print(f"发生错误: {e}")

if __name__ == "__main__":
    create_database()